package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService cartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart cart){
        log.info("[添加购物车 cart:{}]",cart);
        // 1.获取当前用户的ID(ThreadLocal取)，赋值给cart对象
        cart.setUserId(BaseContext.getCurrentId());
        // 2.根据当前用户的ID及菜品ID或套餐ID，查询购物车对象,一定要找到对应的SQL
        //select * from shopping_cart where user_id = 1 and dish_id = 1
        //select * from shopping_cart where user_id = 1 and setmeal_id = 1
        // 一个用户，一道菜，肯定是一条记录
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        if(cart.getDishId() != null){
            // 此时肯定是菜品
            queryWrapper.eq(ShoppingCart::getDishId,cart.getDishId());
        }else{
            // 此时肯定是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,cart.getSetmealId());
        }
        ShoppingCart carteOne = cartService.getOne(queryWrapper);
        if(carteOne != null){
            // 3.如果购物车在，原数量+1
            carteOne.setNumber(carteOne.getNumber()+1);
            // 更新数据库
            cartService.updateById(carteOne);
        }else{
            // 4.如果不在，添加到购物车，默认数量是1
            //此时存数据库的从用户那边传过来的cart对象
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            cartService.save(cart);
            // 把存到数据库的Cart对象，临时赋值给cartOne,目的就是为了返回
            carteOne = cart;
        }
        // 最终我们要返回购物车对象
        return R.success(carteOne);
    }

    // 查看购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        // 根据用户ID，按创建日期升序排列,获取到就很多数据
        // //select * from shopping_cart where user_id = 1605479590380236802 order by create_time
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCartList = cartService.list(queryWrapper);
        return R.success(shoppingCartList);
    }

    @DeleteMapping("/clean")
    public R<String> clean(){
        // 根据用户ID，清空购物车
        // delete from shopping_cart where user_id = 1605479590380236802
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        cartService.remove(queryWrapper);
        return R.success("清空购物车成功！");
    }



}
